import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
Vue.use(VueRouter);
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login.vue"),
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
    meta: { auth: true },

    children: [
      {
        path: "/Welcome",
        name: "Welcome",
        component: () => import("../views/Welcome.vue"),
        meta: {
          isShow: false
        }
      },
      {
        path: "/users",
        name: "users",
        component: () => import("../views/users/index.vue"),
        meta: {
          title: "用户管理",
          title2: "用户列表",
          isShow: true
        }
      },
      {
        path: "/roles",
        name: "roles",
        component: () => import("../views/roles/index.vue"),
        meta: {
          title: "权限管理",
          title2: "角色列表",
          isShow: true
        }
      },
      {
        path: "/rights",
        name: "rights",
        component: () => import("../views/roles/rights.vue"),
        meta: {
          title: "权限管理",
          title2: "权限列表",
          isShow: true
        }
      },
      {
        path: "/goods",
        name: "goods",
        component: () => import("../views/goods/index.vue"),
        meta: {
          title: "商品管理",
          title2: "商品列表",
          isShow: true
        },
      },
      {
        path: "/add",
        name: "add",
        component: () => import("../views/goods/add.vue"),
        meta: {
          title: "商品管理",
          title2: "添加商品",
          isShow: true
        },
      },
      {
        path: "/params",
        name: "params",
        component: () => import("../views/goods/params.vue"),
        meta: {
          title: "商品管理",
          title2: "分类参数",
          isShow: true
        }
      },
      {
        path: "/categories",
        name: "categories",
        component: () => import("../views/goods/categories.vue"),
        meta: {
          title: "商品管理",
          title2: "商品分类",
          isShow: true
        }
      },
      {
        path: "/orders",
        name: "orders",
        component: () => import("../views/orders/index.vue"),
        meta: {
          title: "订单管理",
          title2: "订单列表",
          isShow: true
        }
      },
      {
        path: "/reports",
        name: "reports",
        component: () => import("../views/reports/index.vue"),
        meta: {
          title: "数据统计",
          title2: "数据报表",
          isShow: true
        }
      },

    ]
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  console.log('to:', to)
  //第一步：是否需要鉴权
  let flag = to.matched.some(item => item.meta.auth)
  if (flag) {
    //第二步：获取token
    const token = sessionStorage.getItem('token');
    if (!token) {
      return next({
        //要跳转的路由
        path: '/login',
        //通过query携带要传递的参数,query是一个对象
        query: {
          redirect: to.fullPath
        }
      })
    }
    //有token,就直接放行
    next()

  } else {
    next()
  }



})

export default router;
